#include "tile.h"
#include "constants.h"
#include "functions.h"
#include "globals.h"

SDL_Rect Tile::clips[TILE_SPRITES];

Tile::Tile(int x, int y, int tileType)
{
    box.x = x;
    box.y = y;

    box.w = TILE_WIDTH;
    box.h = TILE_HEIGHT;

    type = tileType;

    color = 0;
}

Tile::~Tile()
{

}

void Tile::clip_tiles()
{
    //Clip the sprite sheet

    clips[TILE_GRASS].x = 0;
    clips[TILE_GRASS].y = 0;
    clips[TILE_GRASS].w = TILE_WIDTH;
    clips[TILE_GRASS].h = TILE_HEIGHT;

    clips[TILE_STONEFLOOR].x = 64;
    clips[TILE_STONEFLOOR].y = 0;
    clips[TILE_STONEFLOOR].w = TILE_WIDTH;
    clips[TILE_STONEFLOOR].h = TILE_HEIGHT;

    clips[TILE_BUSH].x = 128;
    clips[TILE_BUSH].y = 0;
    clips[TILE_BUSH].w = TILE_WIDTH;
    clips[TILE_BUSH].h = TILE_HEIGHT;
}

void Tile::draw()
{
    if(check_collision(cam.get_cam(), box))
    {
        apply_surface(box.x, box.y, tileSheet, screen, &clips[type]);

        if (debug)
        {
            if (color != 0)
            {
                draw_box(x_to_tile(box.x) * TILE_WIDTH, y_to_tile(box.y) * TILE_HEIGHT, TILE_WIDTH, TILE_HEIGHT, color, true);
            }

            draw_box(box.x, box.y, box.w, box.h, 0xFFFFFFCC, false);
        }
    }
}

int Tile::get_type()
{
    return type;
}

SDL_Rect Tile::get_box()
{
    return box;
}

void Tile::drawBox(Uint32 color)
{
    this->color = color;
}
